#include "fuseitem.h"

#include <QPainter>
Fuse::Fuse(bool blown, QGraphicsItem *parent)
    : CircuitComponent(parent)
    , m_blown(blown)    
{
    m_text = "";
}

QPointF Fuse::inputPort() const
{
    return QPointF(-25, 0);
}

QPointF Fuse::outputPort() const
{
    return QPointF(25, 0);
}

void Fuse::setBlown(bool blown)
{
    m_blown = blown;
    update();
}

void Fuse::setText(const QString &text)
{
    m_text = text;
}

void Fuse::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    painter->setRenderHint(QPainter::Antialiasing);
    painter->setPen(QPen(m_blown ? Qt::red : QColor(34,163,169,255), 2));

    // 绘制主体
    painter->drawRect(-20, -8, 40, 16);    

    // 状态指示
    painter->drawLine(-15, 0, 15, 0);
    if(m_blown) {
        painter->drawLine(-10, -5, 10, 5);
    }

    // 绘制端口
    painter->setPen(QColor(m_blown ? Qt::red : QColor(34,163,169,255)));
    painter->drawRect(-30, -5, 10, 10); // 输入端口
    painter->drawRect(20, -5, 10, 10);  // 输出端口

    //绘制文本信息
    painter->setPen(QPen(Qt::white));
    painter->drawText(QRect(-40, -28, 120, 20), Qt::AlignLeft,m_text);
}
